//
//  UIButton+backgroundColorForState.m
//  Zhidao
//
//  Created by xiaoqiang on 13-8-23.
//  Copyright (c) 2013年 Baidu. All rights reserved.
//

#import "UIButton+backgroundColorForState.h"
#import <QuartzCore/QuartzCore.h>
#import "UIImage+Additions.h"

@implementation UIButton (backgroundColorForState)

- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state
{
    UIView *colorView = [[UIView alloc] initWithFrame: self.frame];
    colorView.backgroundColor = color;
    
    UIGraphicsBeginImageContext(colorView.bounds.size);
    
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    if(context)  [colorView.layer renderInContext: context];
    
    UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    [self setBackgroundImage: colorImage forState: state];
    
    colorImage = nil;
    colorView = nil;
    
}

- (void)setALBackgroundColor:(UIColor *)color forState:(UIControlState)state
{
    UIView *colorView = [[UIView alloc] initWithFrame: self.frame];
    colorView.backgroundColor = color;
    
    if (colorView.frame.size.width == 0 || colorView.frame.size.height == 0) {
        colorView.frame = CGRectMake(0, 0, 1, 1);
    }
    UIGraphicsBeginImageContext(colorView.bounds.size);
    
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    if(context)  [colorView.layer renderInContext: context];
    
    UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    [self setBackgroundImage:[colorImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forState: state];
    colorImage = nil;
    colorView = nil;
    
}


- (void)setAutoHighlightBackgroundImage:(UIImage *)image
{
    [self setBackgroundImage:image forState:UIControlStateNormal];
    [self setBackgroundImage:[image highLightColor] forState:UIControlStateHighlighted];
}
- (void)setCornerRadius:(CGFloat)radius color:(UIColor *)color borderColor:(UIColor *)borderColor{
    
    [self.layer setMasksToBounds:YES];
    [self.layer setCornerRadius:radius]; //设置矩圆角半径
    [self.layer setBorderWidth:0.8];   //边框宽度
    CGColorRef colorref = borderColor.CGColor;
    [self.layer setBorderColor:colorref];//边框颜色
}


- (void)setBackgroundColorWithCornerRadius:(CGFloat)radius color:(UIColor *)color borderColor:(UIColor *)borderColor forState:(UIControlState)state
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(radius*5, radius*5), NO, [[UIScreen mainScreen] scale]);
    
    UIBezierPath *roundedRectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, radius*5, radius*5) cornerRadius:radius];
    [color setFill];
    [roundedRectPath fill];
    if (borderColor) {
        [borderColor setStroke];
        roundedRectPath.lineWidth = 0.5;
        [roundedRectPath stroke];
    }
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self setBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(radius*1.5, radius*1.5, radius*1.5, radius*1.5)] forState:state];
}

@end
